package com.zkzx.company.base.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zkzx.common.company.model.CompanyListVO;
import com.zkzx.common.company.param.CompanyFilterParam;
import com.zkzx.common.global.Global;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.company.base.service.CompanyService;
import com.zkzx.company.base.service.constants.CompanyStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年05月20日 16时09分
 */
@Slf4j
@Component
public class CompanyJob {

    @Autowired
    private CompanyService companyService;

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("companyStatusJobHandler")
    public void companyStatusJobHandler(){
        XxlJobHelper.log("执行企业信息状态查询更新");

        List<String> statusList = new ArrayList<>();
        // 审核未通过
        statusList.add(CompanyStatusEnum.NO_REVIEW.name());
        // 审核中
        statusList.add(CompanyStatusEnum.WAIT.name());
        CompanyFilterParam filterParam = new CompanyFilterParam();
        filterParam.setStatusList(statusList);


        // 分页处理 每次查询100条
        RequestFilterParams params = new RequestFilterParams();
        params.setPage(Global.ONE);
        params.setPerPage(100);
        params.setField("createTime");
        params.setOrder("desc");

       int totalNum = companyService.find(filterParam, params).getTotalNum();

        log.info("执行企业信息状态查询更新:{}", totalNum);

        // 当查询到有数据
        if(totalNum <= Global.ZERO){
            return;
        }

        // 总条数 处于 每页数量 向上取整 101 / 100 = 2.0
        int num = (int) Math.ceil((float)totalNum/100);

        for (int i = 1; i <= num; i++) {
            params.setPage(i);
            PageResult<CompanyListVO> result = companyService.find(filterParam, params);
            result.getResult().forEach(vo -> companyService.updateToWh(vo));
        }

    }
}
